﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace PKSpeed
{
    /// <summary>
    /// 错误记录及处理类
    /// </summary>
    public class ErrorLog
    {
        //获取app.config路径
        private static readonly string Path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

        /// <summary>
        /// 将当前用户及当前指令记入配置文件
        /// </summary>
        public static void  OperationStart()
        {
            var xml = new XmlDocument();  
            xml.Load(Path);//打开配置文件
            var errorlog = xml.GetElementsByTagName("errorlog")[0];//获取errorlog节点
            var errorelement = xml.CreateElement("error");//添加节点
            errorelement.SetAttribute("userName", Program.CurrentCommand.CurrentUser);//添加用户名和用户指令记录
            errorelement.SetAttribute("commandStr", Program.CurrentCommand.CurrentInput);
            errorelement.SetAttribute("path", Program.CurrentCommand.CurrentPath);
            errorlog.AppendChild(errorelement);
            xml.Save(Path);
        }

        /// <summary>
        /// 将当前用户及当前指令移出配置文件
        /// </summary>
        public static void OperationEnd()
        {
            var xml = new XmlDocument();
            xml.Load(Path);//打开配置文件
            var errorlog = xml.GetElementsByTagName("errorlog")[0];//获取errorlog节点
            var errors = xml.GetElementsByTagName("error");//获取error节点列表
            if(errors.Count==0) //判断是否存在error节点
            {
                return;
            }
            for (int i = 0; errors[i] != null;i++ )  //去除当前用户当前命令节点
            {
                string userName = errors[i].Attributes["userName"].Value;
                string commandStr = errors[i].Attributes["commandStr"].Value;
                if(userName==Program.CurrentCommand.CurrentUser&&
                    commandStr==Program.CurrentCommand.CurrentInput) //找到当前用户记录命令的节点
                {
                    errorlog.RemoveChild(errors[i]);//移出节点
                }
            }
            xml.Save(Path);
        }

        /// <summary>
        /// 当前用户上次操作是否出错
        /// </summary>
        /// <returns>上次出错返回true，否则返回false</returns>
        public static bool ErrorTest()
        {
            string currentUser = Program.CurrentCommand.CurrentUser;
            var xml = new XmlDocument();
            xml.Load(Path);//打开配置文件
            var errors = xml.GetElementsByTagName("error");//获取error节点列表
            if (errors.Count == 0) //判断是否存在error节点
            {
                return false;
            }
            for (int i = 0; errors[i] != null; i++)  //去除当前用户当前命令节点
            {
                string userName = errors[i].Attributes["userName"].Value;
                if(userName==currentUser)  //找到当前用户记录错误的节点
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 修复错误
        /// </summary>
        public static void Fix()
        {
            string currentUser = Program.CurrentCommand.CurrentUser;
            var xml = new XmlDocument();
            xml.Load(Path);//打开配置文件
            var errorlog = xml.GetElementsByTagName("errorlog")[0];//获取errorlog节点
            var errors = xml.GetElementsByTagName("error");//获取error节点列表
            for (int i = 0; errors[i] != null; i++)  //去除当前用户当前命令节点
            {
                string userName = errors[i].Attributes["userName"].Value;
                string commandStr = errors[i].Attributes["commandStr"].Value;
                if (userName == currentUser)//找到当前用户记录错误的节点
                {
                    Program.CurrentCommand.CurrentPath = errors[i].Attributes["path"].Value;
                    errorlog.RemoveChild(errors[i]); //移出错误节点
                    Console.WriteLine(commandStr);
                    //重新执行命令
                    if (Program.CurrentCommand.Start(commandStr) &&
                    Program.CurrentCommand.Get() &&
                    Program.CurrentCommand.Execute())
                    {
                        Console.WriteLine("Error fixed");
                    }
                    Program.CurrentCommand.CurrentPath = "/";
                }
            }
            xml.Save(Path);
        }
    } 
}
